	mui.init();
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    var currentPage1=1;      //已发货
    var currentPage2=1;      //未发货
    var pageSize=10;
    var total_page1=0;
    var total_count1=0;
    var total_page2=0;
    var total_count2=0;

	//阻尼系数
	var deceleration = mui.os.ios ? 0.003 : 0.0009;
	mui('.mui-scroll-wrapper').scroll({
		bounce: false,
		indicators: false, //是否显示滚动条
		deceleration: deceleration
	});
	mui.plusReady(function() {		
		refresh_data();
		mui(".mui-bar").on('tap','.mui-action-back.mui-icon-left-nav.mui-pull-left',function(){
			mui.back();
		})
		document.getElementById('refresh').addEventListener('tap', function() {
			location.href=location;
		})
		
		document.getElementById('cancel').addEventListener('tap', function() {
			document.getElementById('order_id').value='';
			document.getElementById('shipping_info').style.display="none";
		})
		document.getElementById('cancel_order').addEventListener('tap', function() {
			document.getElementById('oid').value='';
			document.getElementById('remark').value='';
			document.getElementById('cancel_order_info').style.display="none";
		})
		
		
		document.getElementById('do_send').addEventListener('tap', function() {
			var Info={
				'order_id':document.getElementById('order_id').value.trim(),
				'shipping_name':document.getElementById('shipping_name').value.trim(),
				'invoice_no':document.getElementById('invoice_no').value.trim(),
				'_token':userinfo.token
			};
			if(Info.order_id==''){
				mui.alert('未获取到订单id');
				return false;
			}
			if(Info.shipping_name==''){
				mui.alert('快递公司名称不能为空');
				return false;
			}
			if(Info.invoice_no==''){
				mui.alert('快递单号不能为空');
				return false;
			}
			mui.ajax({
					url: apiurl + "Store/do_delivery/",
					data: Info,
					async: true,
					dataType: 'json',
					crossDomain: true, //强制使用5+跨域
					type: 'post',
					timeout: 10000,
					success: function(res) {
						console.log(JSON.stringify(res));
						if(res.status ==-3){
							mui.toast(res.msg);
							app.out();
							return false;
						}
						
						// 请求成功
						if(res.status==0) {
							mui.toast(res.msg);
						} else {
							mui.toast(res.msg);
							document.getElementById('order_id').value='';
							document.getElementById('shipping_info').style.display="none";
                            setTimeout(function() {
								location.href=location;
							}, 100);
							
						}
					},
					error: function(xhr, type, errorThrown) {
						//console.log(JSON.stringify(xhr));
						//console.log(JSON.stringify(type));
						//console.log(JSON.stringify(errorThrown));
						mui.toast('获取数据失败！');
					}
				});
				
		})


		document.getElementById('do_cancel_order').addEventListener('tap', function() {
			var Info={
				'order_id':document.getElementById('oid').value.trim(),
				'remark':document.getElementById('remark').value.trim(),
				'_token':userinfo.token
			};
			if(Info.order_id==''){
				mui.alert('未获取到订单id');
				return false;
			}
			if(Info.remark.length<5){
				mui.alert('取消订单的理由不能少于5个字');
				return false;
			}
			
			mui.ajax({
					url: apiurl + "Store/store_del_order/",
					data: Info,
					async: true,
					dataType: 'json',
					crossDomain: true, //强制使用5+跨域
					type: 'post',
					timeout: 10000,
					success: function(res) {
						console.log(JSON.stringify(res));
						if(res.status ==-3){
							mui.toast(res.msg);
							app.out();
							return false;
						}
						
						// 请求成功
						if(res.status==0) {
							mui.toast(res.msg);
						} else {
							mui.toast(res.msg);
							document.getElementById('oid').value='';
							document.getElementById('remark').value='';
							document.getElementById('cancel_order_info').style.display="none";
                            setTimeout(function() {
								location.href=location;
							}, 500);
							
						}
					},
					error: function(xhr, type, errorThrown) {
						//console.log(JSON.stringify(xhr));
						//console.log(JSON.stringify(type));
						//console.log(JSON.stringify(errorThrown));
						mui.toast('获取数据失败！');
					}
				});
				
		})
		
	});

	//加载数据
	function refresh_data(){
		currentPage1=1;
		currentPage2=1; 
        mui.ajax({
					url: apiurl + "Store/store_order_list/",
					data: {'p':1,'size':pageSize,'user_id':uid,'_token':userinfo.token},
					async: true,
					dataType: 'json',
					crossDomain: true, //强制使用5+跨域
					type: 'get',
					timeout: 10000,
					success: function(res) {
						console.log(JSON.stringify(res));
						// 请求成功
						if(res.status ==-3){
							mui.toast(res.msg);
							app.out();
							return false;
						} else {
							$('#item1 ul').empty();
							$('#item2 ul').empty();
							var data_1=assemble_data(res.resource1,0);
							$('#item1 ul').append(data_1);
							total_page1=res.total_page1;
						    total_count1=res.sum1;
						
							var data_2=assemble_data(res.resource2,0);
							$('#item2 ul').append(data_2);
							total_page2=res.total_page2;
						    total_count2=res.sum2;

						}
					},
					error: function(xhr, type, errorThrown) {
						//console.log(JSON.stringify(xhr));
						//console.log(JSON.stringify(type));
						//console.log(JSON.stringify(errorThrown));
						mui.toast('获取数据失败！');
					}
				});
				
				//循环初始化所有下拉刷新，上拉加载。
				mui.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) {
					mui(pullRefreshEl).pullToRefresh({
						up: {
							callback: function() {
								var self = this;
								setTimeout(function() {
									var type;
									var cup;
									var ind=index+1;
									if(index==0){
										currentPage1++;
										type=1;
										cup=currentPage1;
										
									}
									if(index==1){
										currentPage2++;
										cup=currentPage2;
										type=2;
										
									}
						
								    mui.ajax({
										url: apiurl + "Store/store_order_list/",
										data: {
											'p': cup,
											'size': pageSize,
											'user_id': uid,
											'type': type,
											'_token':userinfo.token
										},
										async: true,
										dataType: 'json',
										crossDomain: true, //强制使用5+跨域
										type: 'get',
										timeout: 10000,
										success: function(res) {
											//console.log(JSON.stringify(res));
											// 请求成功
											if(res.status ==-3){
												mui.toast(res.msg);
												app.out();
												return false;
											} else {
												switch(ind) {
													case 1:
														var data_1 = assemble_data(res.resource,11);
														$('#item1 ul').append(data_1);
														if(currentPage1 < total_page1) {
															self.endPullUpToRefresh(false); /*能上拉*/
														} else {
															self.endPullUpToRefresh(true); /*不能上拉*/
														}
														break;
													case 2:
														var data_2 = assemble_data(res.resource,11);
														$('#item2 ul').append(data_2);
														if(currentPage2 < total_page2) {
															self.endPullUpToRefresh(false); /*能上拉*/
														} else {
															self.endPullUpToRefresh(true); /*不能上拉*/
														}
														break;
												}
								
											}
										},
										error: function(xhr, type, errorThrown) {
											//console.log(JSON.stringify(xhr));
											//console.log(JSON.stringify(type));
											//console.log(JSON.stringify(errorThrown));
											mui.toast('获取数据失败！');
										}
									});
										return ;
				
								}, 1000);
							}
						}
					});
				});

	}
	    
	function assemble_data(data,j){
		/*
		 * <li class="mui-table-view-cell">
							<div class="goodsbox">
								<div class="goods-top">
									<div class="goods-top-left">
										<img src="../../../img/shop.png">
									</div>
									<div class="goods-top-right">
										<h3>苹果iPhone7全网通4G手机中国红</h3>
										<p class='mui-ellipsis'>价格：3278</p>
										<p class='mui-ellipsis'>库存：3</p>
									</div>
									<div class="goods-triangle"><p class="goods-num">01</p></div>
									<div class="goods-money">合计：9999</div>
									<div style="clear: both;"></div>
								</div>
								<div class="goods-main">
									<p>订单编号：2018062901</p>
									<p>姓名：张三</p>
									<p>联系电话：2018062901</p>
									<p>收货地址：2018062901</p>
									<p>备注信息：2018062901</p>
								</div>
								<button class="shop-goods">发货</button>
							</div>
							<span class="mui-pull-right" id="confirm" style="display: none;"></span>
						</li>
		 */
		var htm='';
		for(z in data){
			htm+='<li class="mui-table-view-cell">';
			htm+='<div class="goodsbox">';
			htm+='<div class="goods-top">';
			htm+='<div class="goods-top-left">';
			if(data[z]['original_img'].indexOf(upurl+'Uploads') !== -1){
				var img_path=data[z]['original_img'];
			}
			else{
				var img_path=upurl+data[z]['original_img'];
			}
			htm+='<img src="'+img_path+'">';
			htm+='</div>';
			htm+='<div class="goods-top-right">';
			htm+='<h3>'+data[z]['goods_name']+'</h3>';
			htm+='<p class="mui-ellipsis">价格：'+data[z]['goods_price']+'</p>';
			htm+='<p class="mui-ellipsis">数量：'+data[z]['goods_num']+'</p>';
			htm+='<p class="mui-ellipsis">运费：'+data[z]['shipping_price']+'</p>';
			htm+='</div>';
			j++;
			if(j<10){
				htm+='<div class="goods-triangle"><p class="goods-num">0'+j+'</p></div>';
			}
			htm+='<div class="goods-money">合计：'+data[z]['order_amount']+'</div>';
			htm+='<div style="clear: both;"></div>';
			htm+='</div>';
			
			htm+='<div class="goods-main">';
			htm+='<p>订单编号：'+data[z]['order_sn']+'</p>';
			htm+='<p>下单时间：'+data[z]['add_time']+'</p>';
			if(data[z]['shipping_status']==1){
				htm+='<p>发货时间：'+data[z]['shipping_time']+'</p>';
			}

			if(data[z]['shipping_status']==0){
				htm+='<p>状态：<font style="color:#fab538">未发货</font></p>';
			}
			if(data[z]['shipping_status']==1 && data[z]['order_status']==1){
				htm+='<p>状态：<font style="color:#fab538">未收货</font></p>';
			}
			if(data[z]['shipping_status']==1 && data[z]['order_status']==2){
				htm+='<p>收货时间：'+data[z]['confirm_time']+'</p>';
				htm+='<p>状态：<font style="color:#fab538">已收货</font></p>';
			}
			
			htm+='<p>收货人姓名：'+data[z]['consignee']+'</p>';
			htm+='<p>收货人电话：'+data[z]['mobile']+'</p>';
			htm+='<p>收货地址：'+data[z]['province']+' '+data[z]['city']+' '+data[z]['district']+' '+data[z]['address']+'</p>';
			htm+='<p>备注信息：'+data[z]['user_note']+'</p>';
			htm+='</div>';
			if(data[z]['shipping_status']==0){
				htm+='<div class="shop-goods">';
				htm+='<button onclick="deliver(\''+data[z]['order_id']+'\')">发货</button>';
				htm+='<button onclick="del_order(\''+data[z]['order_id']+'\')" style="background: red;color:#fff;">取消订单</button>';
				htm+='<div style="clear: both;"></div></div>';
			}
			htm+='</div>';
			htm+='<span class="mui-pull-right" id="confirm" style="display: none;"></span>';
			htm+='</li>';
		}
		return htm;
	}
	
	
	//发货
	function deliver(order_id){
		document.getElementById('order_id').value=order_id;
		document.getElementById('shipping_info').style.display="block";
	}
	
	//取消订单
	function del_order(order_id){
		document.getElementById('oid').value=order_id;
		document.getElementById('cancel_order_info').style.display="block";
	}
	
	
	
		